<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Title</title>
</head>
<body>
</body>
<script>
	// 多叉树指定层节点的个数
	function getNodeNum(tree,depth){
		let nodeNum = 0
		let queue = []
		for(const item of tree){
			item.depth = 1
			queue.push(item)
		}
		let currentDepth = 0
		while (currentDepth <= depth && queue.length > 0){
			const currentNode = queue.shift()
			currentDepth = currentNode.depth
			if (currentNode.children){
				for(const item of currentNode.children){
					item.depth = currentNode.depth + 1
					queue.push(item)
				}
			}
			if (currentDepth === depth){
				nodeNum++
			}
		}
		return nodeNum
	}
	{
		const tree = [];
		console.log(getNodeNum(tree, 1)); // 预期输出: 0
	}
	
	{
		const tree = [{ id: 'A' }];
		console.log(getNodeNum(tree, 1)); // 预期输出: 1
		console.log(getNodeNum(tree, 2)); // 预期输出: 0
	}
	
	{
		const tree = [
			{
				id: 'A',
				children: [
					{ id: 'B' },
					{ id: 'C' }
				]
			}
		];
		console.log(getNodeNum(tree, 2)); // 预期输出: 2（当前代码无法通过，需修正）
		
	}
	{
		const tree = [
			{
				id: 'A',
				children: [
					{
						id: 'B',
						children: [
							{ id: 'D' },
							{ id: 'E' }
						]
					},
					{ id: 'C' }
				]
			}
		];
		console.log(getNodeNum(tree, 3)); // 预期输出: 2（当前代码无法通过，需修正）
		
	}
	{
		const tree = [{ id: 'A' }];
		console.log(getNodeNum(tree, 5)); // 预期输出: 0
	}
	{
		const tree = [{ id: 'A' }];
		console.log(getNodeNum(tree, 0)); // 预期输出: 0
		
	}
</script>
</html>